package com.herui.iot.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by amanda.shan on 2020/3/23.
 */
public class DateUtils {

    /**
     * 判断年月日是否相同
     *
     * @param d1
     * @param d2
     * @return
     */
    public static boolean sameYMDate(Date d1, Date d2) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMM");
        //fmt.setTimeZone(new TimeZone()); // 如果需要设置时间区域，可以在这里设置
        return fmt.format(d1).equals(fmt.format(d2));
    }

    /**
     * 只保留到小时 别的清0
     *
     * @param time
     * @return
     */
    public static long getYMDH(long time) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHH");
        String format = fmt.format(new Date(time));
        try {
            return fmt.parse(format).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 只保留到天 别的清0
     *
     * @param time
     * @return
     */
    public static long getYMD(long time) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        String format = fmt.format(new Date(time));
        try {
            return fmt.parse(format).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    // 获取当天的开始时间
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    // 获取本周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfWeek(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    // 获取本周的结束时间
    public static Date getEndDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.setTime(getBeginDayOfWeek(date));
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    // 获取本月的开始时间
    public static Date getBeginDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(getNowYear(date), getNowMonth(date) - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    // 获取本月的结束时间
    public static Date getEndDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(getNowYear(date), getNowMonth(date) - 1, 1);
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(getNowYear(date), getNowMonth(date) - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取当月的总共天数
     *
     * @param date
     * @return
     */
    public static int getTotalDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        Date endDayOfMonth = getEndDayOfMonth(new Date());
        calendar = Calendar.getInstance();
        calendar.setTime(endDayOfMonth);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    // 获取今年是哪一年
    public static Integer getNowYear(Date date) {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(Calendar.YEAR));
    }

    // 获取本月是哪一月
    public static int getNowMonth(Date date) {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(Calendar.MONTH) + 1;
    }

    // 获取某个日期的开始时间
    public static Date getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 获取某个日期的结束时间
    public static Date getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    // 获取星期
    public static String dateToWeek(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String sToHHmmss(int mss){
        long hours = (mss % ( 60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % ( 60 * 60)) /60;
        long seconds = mss % 60;

        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }

    public static String sToMMss(int mss){
        long hours = (mss % ( 60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % ( 60 * 60)) /60;
        long seconds = mss % 60;

        return String.format("%02d:%02d", minutes, seconds);
    }

    public static void main(String[] args) {

        long time = System.currentTimeMillis();
//        Date date1 = DateUtils.getBeginDayOfWeek(new Date(time));
//        Date date2 = DateUtils.getEndDayOfWeek(new Date(time));

        Date date1 = DateUtils.getBeginDayOfMonth(new Date(time));
        Date date2 = DateUtils.getEndDayOfMonth(new Date(time));

        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
        System.out.println(format.format(date1));
        System.out.println(format.format(date2));

        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.MONTH));
    }

}
